package com.dayuan.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dayuan.bean.Goods;
import com.dayuan.constant.AjaxMsg;
import com.dayuan.service.GoodsService;
import com.dayuan.vo.ResultVo;

@Controller
@RequestMapping("/goods")
public class GoodsController {
	@Resource(name = "goodsService")
	private GoodsService gs;

	private static Logger logger = LoggerFactory.getLogger(GoodsController.class);

	/* 根据商品名称或者直接查询商品详情表所有物品（模糊查询） */
	@RequestMapping(value = "/select.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectGoodsAll(String goodsName) {

		ResultVo resultVo = new ResultVo();

		try {

			List<Goods> goods = gs.selectGoodsAll(goodsName);
			resultVo.setData(goods);

			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.error(e.getMessage());
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络不稳定，请稍后再试");
			return resultVo;
		}

	}

	/* 根据商品id查询商品详情表所有物品（模糊查询） */
	@RequestMapping(value = "/selectbyid.im", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public ResultVo selectGoodsAllById(HttpSession session,Integer id) {

		ResultVo resultVo = new ResultVo();

		try {
			
			Goods goods = gs.selectGoodsAllById(id);

			resultVo.setData(goods);
			resultVo.setCode(AjaxMsg.SUCCESS.getValue());
			return resultVo;

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.error(e.getMessage());
			resultVo.setCode(AjaxMsg.FAIL.getValue());
			resultVo.setMsg("网络不稳定，请稍后再试");
			return resultVo;
		}

	}
}
